﻿using Dyao.Book.Core;
using Microsoft.Extensions.DependencyInjection;

namespace Foundation.Core
{
    public class CurrentUserSelectParameterValueProvider : ISelectParameterValueProvider, ITransientDependency
    {
        private readonly IServiceProvider _serviceProvider;

        public CurrentUserSelectParameterValueProvider(IServiceProvider serviceProvider)
        {
            this._serviceProvider = serviceProvider;
        }

        public ISelectParameterValue GetValue(IDictionary<string, object> arguments, SelectItemLoadMethodParameter parameter)
        {
            if (parameter.ParameterType == typeof(ICurrentUser))
            {
                var user = _serviceProvider.GetService<ICurrentUser>();

                var formatter = $"id:{user.Id},name:{user.UserName}";

                return new SelectParameterValue(true, user, formatter);
            }

            return new SelectParameterValue();
        }
    }
}
